package org.b3log.symphony.service;

import org.b3log.latke.ioc.Inject;
import org.b3log.latke.logging.Logger;
import org.b3log.latke.repository.RepositoryException;
import org.b3log.latke.repository.annotation.Transactional;
import org.b3log.latke.service.annotation.Service;
import org.b3log.symphony.model.Statictis;
import org.b3log.symphony.repository.StatictisRepository;
import org.json.JSONObject;

@Service
public class StatictisService {

    /**
     * Logger.
     */
    private static final Logger LOGGER = Logger.getLogger(OptionMgmtService.class);

    @Inject
    private StatictisRepository statictisRepository;

    //获取浏览量，只有一条数据，id为1
    @Transactional
    public JSONObject get() {
        final String id="1";
        JSONObject ret = new JSONObject();
        try {
            ret = statictisRepository.get(id);
            if(null == ret){
                LOGGER.warn("浏览量字段为空值！");
                return null;
            }
        } catch (RepositoryException e) {
            LOGGER.error("浏览量字段抛异常RepositoryException！");
            e.printStackTrace();
        }
        LOGGER.info("获取浏览量！");
        return ret;
    }

    @Transactional
    public JSONObject getAdd() {
        final String id="1";
        JSONObject ret = new JSONObject();
        try {
            ret = statictisRepository.get(id);
            if(null == ret){
                LOGGER.warn("浏览量字段为空值！");
                return null;
            }
            statictisRepository.update(id, ret.increment(Statictis.VISIT), Statictis.VISIT);
        } catch (RepositoryException e) {
            LOGGER.error("浏览量字段抛异常RepositoryException！");
            e.printStackTrace();
        }
        LOGGER.info("浏览量加1！");
        return ret;
    }
} 